# !/usr/bin/env python
# -*- coding: utf-8 -*-
from functools import reduce, lru_cache, partial
import time


def add(x, y):
    return x + y


def hello(name):
    return "hello," + str(name)


@lru_cache(maxsize=None)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)


if __name__ == '__main__':
    #  ((((1+2)+3)+4)+5)
    r1 = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
    print(r1)

    # 第一次计算fibonacci(30)时会耗时，但后续调用会立即返回缓存的结果
    start = time.time()
    r1 = fibonacci(300)  # 第一次计算
    end = time.time()
    print(r1, f"运行时间: {end - start}秒")
    start = time.time()
    r1 = fibonacci(300)  # 立即返回缓存的结果
    end = time.time()
    print(r1, f"运行时间: {end - start}秒")

    add_five = partial(add, 5)
    # 使用 add_five 函数
    result = add_five(3)  # 等价于调用 add(5, 3)，结果为 8
    print(result)  # 输出: 8

    world = partial(hello, "world")
    print(world)
